package com.ruoyi.system.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.constant.MqttPushClient;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.media.*;
import com.ruoyi.system.service.*;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/sys/etc")
public class EtcController extends BaseController {

    @Autowired
    private IEtcb4Service etcb4Service;

    @Autowired
    private IObuEf01Service obuEf01Service;

    @Autowired
    private IObuEf04Service obuEf04Service;

    @Autowired
    private IObu0015Service obu0015Service;

    @Autowired
    private IObu0019Service obu0019Service;

    @Autowired
    private ICpcCardService cpcCardService;

    @Autowired
    private ICpcEf01Service cpcEf01Service;

    @Autowired
    private ICpcEf02Service cpcEf02Service;

    @Autowired
    private ICpcEf04Service cpcEf04Service;

    @Autowired
    private MqttPushClient mqttPushClient;

    /**
     * ETC入出口B2帧
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/pushB2/{type}/{hex}")
    public AjaxResult pushB2(@PathVariable("type") String type, @PathVariable("hex") String hex) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = "{\"msgId\":\"2024082917000026\",\"pushTime\":\"2024-08-29T17:40:54\",\"msgIndex\":\"16\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"178\",\"macId\":\"0415998G\",\"serialNumber\":\"1101120110132181\",\"errorCode\":\"0\",\"issuerIdentifier\":\"B1B1BEA911010001\",\"contractType\":\"0\",\"contractVersion\":\"22\",\"dateofIssue\":\"20190608\",\"dateofExpire\":\"20290608\",\"equitmentCV\":\"18\",\"obuStatus\":\"8193\",\"positionStatus\":\"1\",\"obuPosition\":\"0000000000000000\",\"antennaID\":\"1\"}";
        } else {
            str = "{\"msgId\":\"2024082217000054\",\"pushTime\":\"2024-08-22T17:15:17\",\"msgIndex\":\"112\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"178\",\"macId\":\"0415998G\",\"serialNumber\":\"3201622102424294\",\"errorCode\":\"0\",\"issuerIdentifier\":\"BDADCBD532010001\",\"contractType\":\"0\",\"contractVersion\":\"64\",\"dateofIssue\":\"20230531\",\"dateofExpire\":\"20280530\",\"equitmentCV\":\"16\",\"obuStatus\":\"8193\",\"positionStatus\":\"1\",\"obuPosition\":\"FFFFFFDC00000238\",\"antennaID\":\"1\"}";
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Rsu", hex), str);
        return toAjax(1);
    }

    /**
     * ETC入出口B4帧
     *
     * @param id
     * @return
     * @throws MqttException
     */
    @GetMapping(value = "/pushB4/{id}/{hex}")
    public AjaxResult pushB4(@PathVariable("id") Long id, @PathVariable("hex") String hex) throws MqttException {
        Etcb4 etcb4 = etcb4Service.selectEtcb4ById(id);

        ObuEf01 obuEf01 = obuEf01Service.selectObuEf01ByB4Id(id);
        ObuEf01Model obuEf01Model = JSON.parseObject(JSON.toJSONString(obuEf01), ObuEf01Model.class);
        ObuEf01EdModel obuEf01EdModel = ObuEf01EdModel.encoded(obuEf01Model);
        String efo1Str = obuEf01EdModel.toHexStr();
        System.out.println("efo1Str");
        System.out.println(efo1Str);

        Obu0015 obu0015 = obu0015Service.selectObu0015ByB4Id(id);
        Ucard0015Model ucard0015Model = JSON.parseObject(JSON.toJSONString(obu0015), Ucard0015Model.class);
        Ucard0015EdModel ucard0015EdModel = Ucard0015EdModel.encoded(ucard0015Model);
        String crad0015Str = ucard0015EdModel.toHexStr();
        System.out.println("crad0015Str");
        System.out.println(crad0015Str);

        Obu0019 obu0019 = obu0019Service.selectObu0019ByB4Id(id);
        Ucard0019Model ucard0019Model = JSON.parseObject(JSON.toJSONString(obu0019), Ucard0019Model.class);
        ucard0019Model.cEntyTim = obu0019.getcEntyTim().toInstant().toEpochMilli() / 1000;
        ucard0019Model.cGrantTm = obu0019.getcGrantTm().toInstant().toEpochMilli() / 1000;
        System.out.println(ucard0019Model.cEntyTim);
        System.out.println(ucard0019Model.cGrantTm);
        System.out.println(JSON.toJSONString(ucard0019Model));
        Ucard0019EdModel ucard0019EdModel = Ucard0019EdModel.encoded(ucard0019Model);
        String crad0019Str = ucard0019EdModel.toHexStr();
        System.out.println("crad0019Str");
        System.out.println(crad0019Str);

        ObuEf04 obuEf04 = obuEf04Service.selectObuEf04ByB4Id(id);
        Ucard0019Dd39 ucard0019Dd39 = JSON.parseObject(JSON.toJSONString(obuEf04), Ucard0019Dd39.class);
        Ucard0015Dd20 ucard0015Dd20 = JSON.parseObject(JSON.toJSONString(obuEf04), Ucard0015Dd20.class);
        ObuEf04Model obuEf04Model = JSON.parseObject(JSON.toJSONString(obuEf04), ObuEf04Model.class);
        ucard0019Dd39.cEntyTim = ucard0019Dd39.cEntyTim / 1000;
        ucard0019Dd39.cGrantTm = ucard0019Dd39.cGrantTm / 1000;
        obuEf04Model.uc0019 = ucard0019Dd39;
        obuEf04Model.uc0015 = ucard0015Dd20;
        System.out.println("obuEf04Model");
        System.out.println(JSON.toJSONString(obuEf04Model));
        ObuEf04EdModel obuEf04EdModel = ObuEf04EdModel.encoded(obuEf04Model);
        String efo4Str = obuEf04EdModel.toHexStr();
        System.out.println("efo4Str");
        System.out.println(efo4Str);

        JSONObject json = new JSONObject();
        if (etcb4.getType() == 0) {
            String str = "{\"msgIndex\":\"32\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"180\",\"macId\":\"0415998G\",\"serialNumber\":\"1101120110132181\",\"errorCode\":\"0\",\"authMode\":\"0\",\"eandom\":\"\",\"algorithmID\":\"0\",\"cardStatus\":\"0\",\"cardRestMoney\":\"4294731513\",\"readEF04Status\":\"0\",\"ef04Info\":\"\",\"vehicleInfoCostTime\":\"48\",\"cpuCostTime\":\"38\",\"ef04CostTime\":\"0\"}";
            json = JSONObject.parseObject(str);
            json.put("msgId", etcb4.getMsgId());
            json.put("pushTime", DateUtils.getTimeT());
            json.put("vehicleInfo", efo1Str);
            json.put("issuerInfo", crad0015Str);
            json.put("lastStation", crad0019Str);
            json.put("ef04Info", efo4Str);
        } else {
            String str = "{\"msgIndex\":\"128\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"180\",\"macId\":\"0415998G\",\"serialNumber\":\"3201622102424294\",\"errorCode\":\"0\",\"authMode\":\"0\",\"eandom\":\"\",\"algorithmID\":\"0\",\"cardStatus\":\"0\",\"cardRestMoney\":\"2147446787\",\"readEF04Status\":\"0\",\"vehicleInfoCostTime\":\"99\",\"cpuCostTime\":\"95\",\"ef04CostTime\":\"50\"}";
            json = JSONObject.parseObject(str);
            json.put("msgId", etcb4.getMsgId());
            json.put("pushTime", DateUtils.getTimeT());
            json.put("vehicleInfo", efo1Str);
            json.put("issuerInfo", crad0015Str);
            json.put("lastStation", crad0019Str);
            json.put("ef04Info", efo4Str);
        }
        System.out.println("b4数据");
        System.out.println(json);
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Rsu", hex), json.toString());

        return success(json);
    }

    /**
     * ETC入出口B4帧 - new
     *
     * @return
     * @throws MqttException
     */
    @GetMapping(value = "/pushB4New")
    public AjaxResult pushB4New(@RequestBody JSONObject jsonObject) throws MqttException {
        String hex = jsonObject.getString("hex");
        String str15 = jsonObject.getString("str15");
        String str19 = jsonObject.getString("str19");
        String ef01 = jsonObject.getString("ef01");
        String ef04 = jsonObject.getString("ef04");
        JSONObject json = new JSONObject();

        String str = "{\"msgId\":\"2025011017000026\",\"pushTime\":\"2025-01-10T17:40:54\",\"msgIndex\":\"128\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"180\",\"macId\":\"041515P0\",\"serialNumber\":\"3201622102424294\",\"errorCode\":\"0\",\"authMode\":\"0\",\"eandom\":\"\",\"algorithmID\":\"0\",\"cardStatus\":\"0\",\"cardRestMoney\":\"2147446787\",\"readEF04Status\":\"0\",\"vehicleInfoCostTime\":\"99\",\"cpuCostTime\":\"95\",\"ef04CostTime\":\"50\"}";
        json = JSONObject.parseObject(str);
        json.put("vehicleInfo", ef01);
        json.put("issuerInfo", str15);
        json.put("lastStation", str19);
        json.put("ef04Info", ef04);

        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Rsu", hex), json.toString());

        return success(json);
    }

    /**
     * ETC入出口B5帧
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/pushB5/{type}/{hex}")
    public AjaxResult pushB5(@PathVariable("type") String type, @PathVariable("hex") String hex) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = "{\"msgId\":\"2024082917000028\",\"pushTime\":\"2024-08-29T17:40:55\",\"msgIndex\":\"48\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"181\",\"macId\":\"0415998G\",\"serialNumber\":\"1101120110132181\",\"errorCode\":\"18\",\"psamNo\":\"123456789ABC\",\"transTime\":\"20240829174012\",\"transType\":\"9\",\"tac\":\"00000000\",\"iccPaySerial\":\"0\",\"psamTransSerial\":\"0\",\"cardBalance\":\"4294731513\",\"algorithmId\":\"0\",\"mac2\":\"00000000\",\"keyVersion\":\"0\",\"writeEF04Status\":\"0\"}";
        } else {
            str = "{\"msgId\":\"2024082217000056\",\"pushTime\":\"2024-08-22T17:15:18\",\"msgIndex\":\"144\",\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"181\",\"macId\":\"0415998G\",\"serialNumber\":\"3201622102424294\",\"errorCode\":\"0\",\"psamNo\":\"123456789ABC\",\"transTime\":\"20240822171517\",\"transType\":\"9\",\"tac\":\"A6CEFAF6\",\"iccPaySerial\":\"39\",\"psamTransSerial\":\"1021212\",\"cardBalance\":\"2147446786\",\"algorithmId\":\"0\",\"mac2\":\"44430D06\",\"keyVersion\":\"1\",\"writeEF04Status\":\"0\"}";
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Rsu", hex), str);
        return toAjax(1);
    }

    /** ###卡机相关### **/

    /**
     * 外设
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/pushWs/{type}/{hex}")
    public AjaxResult pushWs(@PathVariable("type") String type, @PathVariable("hex") String hex) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = "{\"msgId\":\"2024122618000001\",\"pushTime\":\"2024-12-26T18:07:57\",\"infos\":[{\"deviceType\":\"242\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备支付受理终端F2H(1)离线\"},{\"deviceType\":\"235\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备雾灯EBH(1)离线\"},{\"deviceType\":\"243\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备扫码终端F3H(1)离线\"},{\"deviceType\":\"241\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备票据打印机F1H(1)离线\"},{\"deviceType\":\"230\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备信息显示屏E6H(1)离线\"},{\"deviceType\":\"244\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备语音播报设备F4H(1)离线\"},{\"deviceType\":\"233\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备LED 情报板E9H(1)离线\"},{\"deviceType\":\"234\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备雨棚信号灯EAH(1)离线\"},{\"deviceType\":\"238\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备自助交费设备EEH(1)离线\"},{\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备RSU路侧单元E1H(1)离线\"},{\"deviceType\":\"240\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备非接触IC 卡读写器F0H(1)离线\"},{\"deviceType\":\"240\",\"deviceIndex\":\"16\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备非接触IC 卡读写器F0H(2)离线\"},{\"deviceType\":\"229\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备车辆检测器E5H(1)离线\"},{\"deviceType\":\"237\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备自助发卡设备EDH(1)离线\"},{\"deviceType\":\"226\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备高清车牌图像识别设备E2H(1)离线\"},{\"deviceType\":\"239\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备车道摄像机EFH(1)离线\"},{\"deviceType\":\"231\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备通行信号灯E7H(1)离线\"},{\"deviceType\":\"236\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备称重检测设备ECH(1)离线\"},{\"deviceType\":\"228\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备电动栏杆机E4H(1)离线\"},{\"deviceType\":\"232\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备报警器E8H(1)离线\"},{\"deviceType\":\"227\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备车型识别设备E3H(1)离线\"}]}";
        } else {
            str = "{\"msgId\":\"2024122618000001\",\"pushTime\":\"2024-12-26T18:07:57\",\"infos\":[{\"deviceType\":\"242\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备支付受理终端F2H(1)离线\"},{\"deviceType\":\"235\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备雾灯EBH(1)离线\"},{\"deviceType\":\"243\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备扫码终端F3H(1)离线\"},{\"deviceType\":\"241\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备票据打印机F1H(1)离线\"},{\"deviceType\":\"230\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备信息显示屏E6H(1)离线\"},{\"deviceType\":\"244\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备语音播报设备F4H(1)离线\"},{\"deviceType\":\"233\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备LED 情报板E9H(1)离线\"},{\"deviceType\":\"234\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备雨棚信号灯EAH(1)离线\"},{\"deviceType\":\"238\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备自助交费设备EEH(1)离线\"},{\"deviceType\":\"225\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备RSU路侧单元E1H(1)离线\"},{\"deviceType\":\"240\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备非接触IC 卡读写器F0H(1)离线\"},{\"deviceType\":\"240\",\"deviceIndex\":\"16\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备非接触IC 卡读写器F0H(2)离线\"},{\"deviceType\":\"229\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备车辆检测器E5H(1)离线\"},{\"deviceType\":\"237\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备自助发卡设备EDH(1)离线\"},{\"deviceType\":\"226\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备高清车牌图像识别设备E2H(1)离线\"},{\"deviceType\":\"239\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备车道摄像机EFH(1)离线\"},{\"deviceType\":\"231\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备通行信号灯E7H(1)离线\"},{\"deviceType\":\"236\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备称重检测设备ECH(1)离线\"},{\"deviceType\":\"228\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备电动栏杆机E4H(1)离线\"},{\"deviceType\":\"232\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备报警器E8H(1)离线\"},{\"deviceType\":\"227\",\"deviceIndex\":\"1\",\"state\":\"0\",\"connectStatus\":\"0\",\"runState\":\"1\",\"personalizedData\":\"设备车型识别设备E3H(1)离线\"}]}";
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/STMonitor/DevState/list", hex), str);

        return toAjax(1);
    }

    /**
     * 入出口车牌上报
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/cpsb/{type}/{hex}/{plateNum}/{plateColor}")
    public AjaxResult cpsb(@PathVariable("type") String type, @PathVariable("hex") String hex, @PathVariable("plateNum") String plateNum, @PathVariable("plateColor") Integer plateColor) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = String.format("{\"msgId\":\"20241121134913000015\",\"recvTime\":\"2024-11-21T13:49:12\",\"deviceType\":\"226\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"2\",\"picflag\":\"1\",\"picId\":\"20220408171234567\",\"picTime\":\"2024-12-20T11:47:00\",\"plateNum\":\"%s\",\"plateColor\":\"%d\"}", plateNum, plateColor);
        } else {
            str = String.format("{\"msgId\":\"20241121134913000015\",\"recvTime\":\"2024-11-21T13:49:12\",\"deviceType\":\"226\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"2\",\"picflag\":\"1\",\"picId\":\"20220408171234567\",\"picTime\":\"2024-12-20T11:47:00\",\"plateNum\":\"%s\",\"plateColor\":\"%d\"}", plateNum, plateColor);
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Vlpr", hex), str);

        return toAjax(1);
    }

    /**
     * 入出口车型上报
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/cxsb/{type}/{hex}/{identifyType}/{plateNum}/{plateColor}")
    public AjaxResult cxsb(@PathVariable("type") String type, @PathVariable("hex") String hex, @PathVariable("identifyType") Integer identifyType, @PathVariable("plateNum") String plateNum, @PathVariable("plateColor") Integer plateColor) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = String.format("{\"msgId\":\"20241121134913000015\",\"recvTime\":\"2024-11-21T13:49:12\",\"deviceType\":\"227\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"1\",\"axleFlag\":\"1\",\"axleNum\":\"20220408171234567\",\"axleTime\":\"2024-12-20T11:47:00\",\"identifyType\":\"%d\",\"vehicleAxleInfo\":\"11\",\"vehicleAxleCount\":\"2\",\"plateNum\":\"%s\",\"plateColor\":\"%d\"}", identifyType, plateNum, plateColor);
        } else {
            str = String.format("{\"msgId\":\"20241121134913000015\",\"recvTime\":\"2024-11-21T13:49:12\",\"deviceType\":\"227\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"1\",\"axleFlag\":\"1\",\"axleNum\":\"20220408171234567\",\"axleTime\":\"2024-12-20T11:47:00\",\"identifyType\":\"%d\",\"vehicleAxleInfo\":\"11\",\"vehicleAxleCount\":\"2\",\"plateNum\":\"%s\",\"plateColor\":\"%d\"}", identifyType, plateNum, plateColor);
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Axle", hex), str);

        return toAjax(1);
    }

    /**
     * 入出口环检上报(卡机到达)
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/hjsbdd/{type}/{hex}")
    public AjaxResult hjsbdd(@PathVariable("type") String type, @PathVariable("hex") String hex) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = "{\"msgId\":\"2024081918000125\",\"pushTime\":\"2024-08-19T18:17:31\",\"deviceType\":\"229\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"1\",\"status\":\"1\"}";
        } else {
            str = "{\"msgId\":\"2024081918000125\",\"pushTime\":\"2024-08-19T18:17:31\",\"deviceType\":\"229\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"1\",\"status\":\"1\"}";
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/VeDetector", hex), str);

        return toAjax(1);
    }

    /**
     * 入出口卡机按键取卡
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/ajqk/{type}/{hex}")
    public AjaxResult ajqk(@PathVariable("type") String type, @PathVariable("hex") String hex) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = "{\"msgId\":\"2024112113004230\",\"pushTime\":\"2024-11-21T13:48:50\",\"msgIndex\":\"64\",\"deviceType\":\"237\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"8\",\"mid\":\"2\",\"key\":\"1\"}";
        } else {
            str = "{\"msgId\":\"2024082217000063\",\"pushTime\":\"2024-08-22T17:16:49\",\"msgIndex\":\"96\",\"deviceType\":\"238\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"7\",\"upLeftStatus\":\"0\",\"downLeftStatus\":\"1\",\"upRightStatus\":\"0\",\"downRightStatus\":\"0\"}";
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/ACS", hex), str);

        return toAjax(1);
    }

    /**
     * 读卡器寻卡成功
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/xunka/{type}/{hex}")
    public AjaxResult xunka(@PathVariable("type") String type, @PathVariable("hex") String hex) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = "{\"msgId\":\"20241121134913000015\",\"recvTime\":\"2024-11-21T13:49:12\",\"deviceType\":\"240\",\"deviceIndex\":\"2\",\"deviceFuncType\":\"211\",\"errorCode\":\"0\",\"cardType\":\"2\",\"cardSer\":\"1301019400163988\"}";
        } else {
            str = "{\"msgId\":\"20241121134913000015\",\"recvTime\":\"2024-11-21T13:49:12\",\"deviceType\":\"240\",\"deviceIndex\":\"2\",\"deviceFuncType\":\"211\",\"errorCode\":\"0\",\"cardType\":\"2\",\"cardSer\":\"3703019100878451\"}";
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Reply/STDevctl/ICReader", hex), str);

        return toAjax(1);
    }

    /**
     * 读卡器读卡成功
     *
     * @return
     * @throws MqttException
     */
    @GetMapping("/dk/{id}/{hex}")
    public AjaxResult dk(@PathVariable("id") Long id, @PathVariable("hex") String hex) throws Exception {
        JSONObject json = new JSONObject();
        CpcCard card = cpcCardService.selectCpcCardById(id);
        if (card.getType() == 0) {
//            String str = "{\"deviceType\":\"240\",\"deviceIndex\":\"2\",\"deviceFuncType\":\"212\",\"errorCode\":\"0\",\"mfef01\":\"CBC4B4A8510100015101016200319024012018120120281130FFFFFFFFFF\",\"mfef02\":\"850000000000000000000000000000A20725A80C19000000007A0F0F01020A5D000000003237425F535430315F313931303330FFFFFFFFFFFFFFFFFFFFFFFFFF\"}";
//            json = JSONObject.parseObject(str);
//            json.put("msgId", card.getMsgId());
//            json.put("recvTime", DateUtils.getTimeT());
//            json.put("df01ef01", efo1Str);
//            json.put("df01ef02", efo2Str);
//            json.put("df01ef04", efo4Str);
            String str = "{\"msgId\":\"20241121141245000015\",\"recvTime\":\"2024-11-21T14:12:44\",\"deviceType\":\"240\",\"deviceIndex\":\"2\",\"deviceFuncType\":\"212\",\"errorCode\":\"0\",\"mfef01\":\"CBC4B4A8510100015101016200319024012018120120281130FFFFFFFFFF\",\"mfef02\":\"850000000000000000000000000000A20725A80C19000000007A0F0F01020A5D000000003237425F535430315F313931303330FFFFFFFFFFFFFFFFFFFFFFFFFF\",\"df01ef01\":\"01BEA94131323335390000000000110116064366F4F8E80202007529060302FFFFFFFF004650FF00000000FEFFFFFFBBA7DDFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\",\"df01ef02\":\"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\",\"df01ef04\":\"0000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\"}";
            json = JSONObject.parseObject(str);
        } else {

            CpcEf01 cpcEf01 = cpcEf01Service.selectCpcEf01ByCId(id);
            CpcEf01Model cpcEf01Model = JSON.parseObject(JSON.toJSONString(cpcEf01), CpcEf01Model.class);
            cpcEf01Model.cEntyTim = cpcEf01.getcEntyTim().toInstant().toEpochMilli() / 1000;
            System.out.println(cpcEf01Model.cEntyTim);
            CpcEf01EdModel cpcEf01EdModel = CpcEf01EdModel.encoded(cpcEf01Model);
            String efo1Str = cpcEf01EdModel.toHexStr();
            System.out.println("cpc_efo1Str");
            System.out.println(efo1Str);

            CpcEf02 cpcEf02 = cpcEf02Service.selectCpcEf02ByCId(id);
            CpcEf02Model cpcEf02Model = convertToCpcEf02Model(cpcEf02);

            CpcEf02EdModel cpcEf02EdModel = CpcEf02EdModel.encoded(cpcEf02Model);
            String efo2Str = cpcEf02EdModel.toHexStr();
            System.out.println("cpc_efo2Str");
            System.out.println(efo2Str);

            CpcEf04 cpcEf04 = new CpcEf04();
            cpcEf04.setCid(id);
            List<CpcEf04> cpcEf04List = cpcEf04Service.selectCpcEf04List(cpcEf04);
            CpcEf04Model model = convertToCpcEf04Model(cpcEf04List);
            CpcEf04EdModel cpcEf04EdModel = CpcEf04EdModel.encoded(model);
            String efo4Str = cpcEf04EdModel.toHexStr();
            System.out.println("cpc_efo4Str");
            System.out.println(efo4Str);

            String str = "{\"deviceType\":\"240\",\"deviceIndex\":\"2\",\"deviceFuncType\":\"212\",\"errorCode\":\"0\",\"mfef01\":\"C9BDB6AB370300013703019100878451012018120120281130FFFFFFFFFF\",\"mfef02\":\"6401FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\"}";
            json = JSONObject.parseObject(str);
            json.put("msgId", card.getMsgId());
            json.put("recvTime", DateUtils.getTimeT());
            json.put("df01ef01", efo1Str);
            json.put("df01ef02", efo2Str);
            json.put("df01ef04", efo4Str);
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Reply/STDevctl/ICReader", hex), json.toString());

        return success(json.toString());
    }

    private CpcEf02Model convertToCpcEf02Model(CpcEf02 cpcEf02) {
        CpcEf02Model model = new CpcEf02Model();

        // 假设 cStanDat 是一个逗号分隔的字符串
        String cStanDatStr = cpcEf02.getcStanDat();
        if (cStanDatStr != null && !cStanDatStr.isEmpty()) {
            // 分割字符串并转换为数组
            model.cStanDat = cStanDatStr.split(",");
        } else {
            // 如果原始字符串为空，则初始化空数组
            model.cStanDat = new String[24];
        }
        model.nProvSum = cpcEf02.getnProvSum();
        model.nGanySum = cpcEf02.getnGanySum();
        model.cLoclTol = cpcEf02.getcLoclTol().intValue();
        model.cMileSum = cpcEf02.getcMileSum().intValue();
        model.cEntyHex = cpcEf02.getcEntyHex();
        model.cEntyTms = cpcEf02.getcEntyTms().toInstant().toEpochMilli() / 1000;
        model.cNewsGay = cpcEf02.getcNewsGay();
        model.cNewsTms = cpcEf02.getcNewsTms().toInstant().toEpochMilli() / 1000;
        model.cNewsTol = cpcEf02.getcNewsTol().intValue();
        model.cNewsLng = cpcEf02.getcNewsLng().intValue();
        model.nStanSum = cpcEf02.getnStanSum();

        return model;
    }

    private CpcEf04Model convertToCpcEf04Model(List<CpcEf04> cpcEf04List) {
        CpcEf04Model model = new CpcEf04Model();

        // 确保不会超出 provPass 数组长度
        int length = Math.min(cpcEf04List.size(), model.provPass.length);

        for (int i = 0; i < length; i++) {
            copyProperties(cpcEf04List.get(i), model.provPass[i]);
        }

        return model;
    }

    private void copyProperties(CpcEf04 source, CpcProv target) {
        target.nProvDat = source.getnProvDat();
        target.nGanySum = source.getnGanySum();
        target.cLoclTol = source.getcLoclTol().intValue();
        target.cMileSum = source.getcMileSum().intValue();
        target.cEntyHex = source.getcEntyHex();
        target.cEntyTms = source.getcEntyTms().toInstant().toEpochMilli() / 1000;
        target.cNewsGay = source.getcNewsGay();
        target.cNewsTms = source.getcNewsTms().toInstant().toEpochMilli() / 1000;
        target.nMachCss = source.getnMachCss();
    }

    /**
     * 扫码支付
     *
     * @return
     * @throws MqttException
     */
    @GetMapping("/pay/{hex}")
    public AjaxResult pay(@PathVariable("hex") String hex) throws MqttException {
        String str = "{\"msgId\":\"20241121134913000015\",\"pushTime\":\"2024-11-21T13:49:12\",\"deviceType\":\"243\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"196\",\"mid\":\"0\",\"reqId\":\"1234567890\",\"code\":\"1234567854645645645\"}";
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Scan", hex), str);

        return toAjax(1);
    }

    /**
     * 寻卡成功
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/xunkaSuccess/{type}/{hex}")
    public AjaxResult xunkaSuccess(@PathVariable("type") String type, @PathVariable("hex") String hex) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = "{\"msgId\":\"20241121141246000016\",\"recvTime\":\"2024-11-21T14:12:45\",\"deviceType\":\"240\",\"deviceIndex\":\"2\",\"deviceFuncType\":\"211\",\"errorCode\":\"0\",\"cardType\":\"2\",\"cardSer\":\"5101016200319024\"}";
        } else {
            str = "{\"msgId\":\"20241121141246000016\",\"recvTime\":\"2024-11-21T14:12:45\",\"deviceType\":\"240\",\"deviceIndex\":\"2\",\"deviceFuncType\":\"211\",\"errorCode\":\"0\",\"cardType\":\"2\",\"cardSer\":\"5101016200319024\"}";
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Reply/STDevctl/ICReader", hex), str);

        return toAjax(1);
    }

    /**
     * 写卡成功
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/xiekaSuccess/{type}/{hex}")
    public AjaxResult xiekaSuccess(@PathVariable("type") String type, @PathVariable("hex") String hex) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = "{\"msgId\":\"20241121141246000017\",\"recvTime\":\"2024-11-21T14:12:45\",\"deviceType\":\"240\",\"deviceIndex\":\"2\",\"deviceFuncType\":\"213\",\"errorCode\":\"0\"}";
        } else {
            str = "{\"msgId\":\"20241121141246000017\",\"recvTime\":\"2024-11-21T14:12:45\",\"deviceType\":\"240\",\"deviceIndex\":\"2\",\"deviceFuncType\":\"213\",\"errorCode\":\"0\"}";
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Reply/STDevctl/ICReader", hex), str);

        return toAjax(1);
    }

    /**
     * 环检上报(卡机驶离)
     *
     * @param type
     * @return
     * @throws MqttException
     */
    @GetMapping("/hjsbsl/{type}/{hex}")
    public AjaxResult hjsbsl(@PathVariable("type") String type, @PathVariable("hex") String hex) throws MqttException {
        String str = "";
        if (Objects.equals(type, "en")) {
            str = "{\"msgId\":\"2024081918000125\",\"pushTime\":\"2024-08-19T18:17:31\",\"deviceType\":\"229\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"1\",\"status\":\"0\"}";
        } else {
            str = "{\"msgId\":\"2024081918000125\",\"pushTime\":\"2024-08-19T18:17:31\",\"deviceType\":\"229\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"1\",\"status\":\"0\"}";
        }
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/VeDetector", hex), str);

        return toAjax(1);
    }

    /**
     * 称重上报
     *
     * @param hex
     * @param weight
     * @param axleNum
     * @return
     * @throws MqttException
     */
    @GetMapping("/czsb/{hex}/{axleNum}/{weight}")
    public AjaxResult czsb(@PathVariable("hex") String hex, @PathVariable("axleNum") Long axleNum, @PathVariable("weight") Long weight) throws MqttException {
        String str = String.format("{\"msgId\":\"2024123022000113\",\"pushTime\":\"2024-12-30T22:42:41\",\"deviceType\":\"236\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"1\",\"weightTime\":\"1735410740\",\"direction\":\"0\",\"axleId\":\"20241228180082\",\"overload\":\"0\",\"speed\":\"5.0\",\"axleNum\":\"%d\",\"weight\":\"%d\",\"axleType\":\"11\",\"axleWeight\":\"1100|800\"}", axleNum, weight);

        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Push/DevInfo/Weight", hex), str);

        return toAjax(1);
    }

    /**
     * 寻ETC卡成功1
     *
     * @param hex
     * @return
     * @throws MqttException
     */
    @GetMapping("/xetckcg1/{hex}")
    public AjaxResult xetckcg1(@PathVariable("hex") String hex) throws MqttException {
        String str = "{\"msgId\":\"20241122132217001684\",\"recvTime\":\"2024-11-22T13:22:46\",\"deviceType\":\"240\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"211\",\"errorCode\":\"0\",\"cardType\":\"1\",\"cardSer\":\"0716230080000482\"}";

        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Reply/STDevctl/ICReader", hex), str);

        return toAjax(1);
    }

    /**
     * 读ETC卡成功
     *
     * @param id
     * @param hex
     * @return
     * @throws MqttException
     */
    @GetMapping("/detccg/{id}/{hex}")
    public AjaxResult detccg(@PathVariable("id") Long id, @PathVariable("hex") String hex) throws MqttException {

        Obu0015 obu0015 = obu0015Service.selectObu0015ByB4Id(id);
        Ucard0015Model ucard0015Model = JSON.parseObject(JSON.toJSONString(obu0015), Ucard0015Model.class);
        Ucard0015EdModel ucard0015EdModel = Ucard0015EdModel.encoded(ucard0015Model);
        String crad0015Str = ucard0015EdModel.toHexStr();
        System.out.println("ucard0015Model");
        System.out.println(JSON.toJSONString(ucard0015Model));
        System.out.println("crad0015Str");
        System.out.println(crad0015Str);

        Obu0019 obu0019 = obu0019Service.selectObu0019ByB4Id(id);
        Ucard0019Model ucard0019Model = JSON.parseObject(JSON.toJSONString(obu0019), Ucard0019Model.class);
        ucard0019Model.cEntyTim = obu0019.getcEntyTim().toInstant().toEpochMilli() / 1000;
        ucard0019Model.cGrantTm = obu0019.getcGrantTm().toInstant().toEpochMilli() / 1000;
        Ucard0019EdModel ucard0019EdModel = Ucard0019EdModel.encoded(ucard0019Model);
        String crad0019Str = ucard0019EdModel.toHexStr();
        System.out.println("ucard0019Model");
        System.out.println(JSON.toJSONString(ucard0019Model));
        System.out.println("crad0019Str");
        System.out.println(crad0019Str);

        String str = "{\"msgId\":\"20241122132218001686\",\"recvTime\":\"2024-11-22T13:22:46\",\"deviceType\":\"240\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"214\",\"errorCode\":\"0\",\"f0002Info\":\"FFFFDEDE\"}";
        JSONObject json = JSONObject.parseObject(str);
        json.put("f0015Info", crad0015Str);
        json.put("f0019Info", crad0019Str);
        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Reply/STDevctl/ICReader", hex), json.toString());
        return toAjax(1);
    }

    /**
     * 读ETC卡成功 - new
     *
     * @return
     * @throws MqttException
     */
    @GetMapping(value = "/pushNewdetccg")
    public AjaxResult pushNewdetccg(@RequestBody JSONObject jsonObject) throws MqttException {
        String hex = jsonObject.getString("hex");
        String str02 = jsonObject.getString("str02");
        String str15 = jsonObject.getString("str15");
        String str19 = jsonObject.getString("str19");

        JSONObject json = new JSONObject();

        String str = "{\"msgId\":\"20241122132218001686\",\"recvTime\":\"2024-11-22T13:22:46\",\"deviceType\":\"240\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"214\",\"errorCode\":\"0\",\"f0002Info\":\"FFFFDEDE\"}";
        json = JSONObject.parseObject(str);
//        json.put("vehicleInfo", str02);
        json.put("f0015Info", str15);
        json.put("f0019Info", str19);

        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Reply/STDevctl/ICReader", hex), json.toString());

        return success(json);
    }

    /**
     * 寻ETC卡成功2
     *
     * @param hex
     * @return
     * @throws MqttException
     */
    @GetMapping("/xetckcg2/{hex}")
    public AjaxResult xetckcg2(@PathVariable("hex") String hex) throws MqttException {
        String str = "{\"msgId\":\"20241122132218001687\",\"recvTime\":\"2024-11-22T13:22:46\",\"deviceType\":\"240\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"211\",\"errorCode\":\"0\",\"cardType\":\"1\",\"cardSer\":\"0716230080000482\"}";

        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Reply/STDevctl/ICReader", hex), str);

        return toAjax(1);
    }

    /**
     * 写（扣款）ETC卡成功
     *
     * @param hex
     * @return
     * @throws MqttException
     */
    @GetMapping("/kketccg/{hex}")
    public AjaxResult kketccg(@PathVariable("hex") String hex) throws MqttException {
        String str = "{\"msgId\":\"20241122132218001688\",\"recvTime\":\"2024-11-22T13:22:47\",\"deviceType\":\"240\",\"deviceIndex\":\"1\",\"deviceFuncType\":\"215\",\"errorCode\":\"0\"}";

        mqttPushClient.publish(false, String.format("E/14/G0055/G0055140440/14012401/%s/Reply/STDevctl/ICReader", hex), str);

        return toAjax(1);
    }

    private List<Integer> replaceLastUnit(List<Integer> units, int lastUnitAmount) {
        List<Integer> result = new ArrayList<>(units);
        result.set(result.size() - 1, lastUnitAmount);
        return result;
    }
}
